package com.biblio.controllers;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;

import javax.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


import com.biblio.dao.EditorDao;
import com.biblio.dom.Editor;
import com.biblio.exceptions.EditorAlreadyExistsException;
import com.biblio.service.EditorService;
import com.biblio.utils.FileUtil;
import com.biblio.utils.ImageUtil;

@Controller
@RequestMapping("/admin")
public class AdminEditorController extends BaseController<Editor>{
	@Autowired EditorDao editorDao;		
	@Autowired EditorService editorService;	

    @RequestMapping("/editor")
    public String createEditor(@ModelAttribute Editor editor ){
        return "admin/formEditor"; 
    } 
    @RequestMapping("/editorSubmit")
    public ModelAndView editorSubmit(@Valid @ModelAttribute Editor editor, BindingResult result ){
		System.out.println("-------------------------->"+editor.getId()+" "+result.hasErrors());
    	if (result.hasErrors()) {
			return new ModelAndView("admin/formEditor");
		}
    	try {
    		editor = editorService.checkEditor(editor,editor.getEditorName(),editor.getEditorWeb());
		} catch (EditorAlreadyExistsException e) {
			e.printStackTrace();
		}	
        return  new ModelAndView("admin/formEditor");
    } 


}
